﻿Public Class IconListView
    Inherits ListView

    Private m_Brush As New SolidBrush(Color.Black)

    Public Property IconList As List(Of Icon)

    Public Sub New()
        MyBase.New()

        Me.LargeImageList = New ImageList
        Me.LargeImageList.ImageSize = New Size(32, 32)

        Me.SmallImageList = New ImageList
        Me.SmallImageList.ImageSize = New Size(20, 20)

        Me.OwnerDraw = True
    End Sub

    Protected Overrides Sub OnDrawItem(e As DrawListViewItemEventArgs)
        If Me.View = System.Windows.Forms.View.LargeIcon OrElse Me.View = System.Windows.Forms.View.SmallIcon Then
            Dim item = e.Item

            If IconList Is Nothing Then
                Return
            End If

            Dim x_icon = e.Bounds.Left + e.Bounds.Width / 2 - IIf(Me.View = System.Windows.Forms.View.LargeIcon, Me.LargeImageList.ImageSize.Width, Me.SmallImageList.ImageSize.Width / 2)

            If item.Selected Then
                m_Brush.Color = Color.FromKnownColor(KnownColor.Desktop)
                e.Graphics.FillRectangle(m_Brush, e.Bounds)
            End If

            e.Graphics.DrawIcon(IconList(e.ItemIndex), x_icon, e.Bounds.Top)
        Else
            MyBase.OnDrawItem(e)
        End If
    End Sub

    Protected Overrides Sub OnPaintBackground(pevent As PaintEventArgs)
        'MyBase.OnPaintBackground(pevent)
    End Sub
End Class
